// pages/myInfo/myInfo.js
const app = getApp()
import { deviceFindAll } from "../../public/api/device.js"
import { userFind,download } from "../../public/api/person.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    translations: {},
    language: '',
    name: '',
    deviceCount: 0,
    faceData: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  
  },

  //查询人员信息
  getUserInfo() {
    let userId = wx.getStorageSync('userInfo').id
    userFind({userId}).then(res => {
      if(res.data.code === 1) {
        let { name,photoId } = res.data.data
        this.setData({
          name,
          photoId: photoId ? photoId : '',
        })
        if(photoId) this.getPhoto(photoId)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  // 查询照片
  getPhoto(photoId) {
    let data = {}
    data.id = photoId
    download(data).then(res => {
      if(res.statusCode === 200) {
        this.setData({faceData: res.data})
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  getDeviceList() {
    let data = {}
    data.uuid = ''
    deviceFindAll(data).then(res => {
      if(res.data.code === 1) {
        this.setData({deviceCount: res.data.data.length})
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  goSubPage(e) {
    let id = e.currentTarget.dataset.id
    switch(id) {
      case "device":
        wx.navigateTo({
          url: '/packageMyInfo/pages/device/device'
        })
        break;
      case "person":
        wx.navigateTo({
          url: '/packageMyInfo/pages/person/person'
        })
        break;
      case "language":
        wx.navigateTo({
          url: '/packageMyInfo/pages/langu/langu'
        })
        break;
      case "set":
        wx.navigateTo({
          url: '/packageMyInfo/pages/set/set'
        })
        break;
      case "detail":
        wx.navigateTo({
          url: '/packageMyInfo/pages/infoDetail/infoDetail'
        })
        break;
      default:
        break;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const translations = app.globalData.translations;
    let language = wx.getStorageSync('language')
    console.log(app.globalData.userInfo);
    this.setData({
      translations: app.globalData.translations,
      language,
      name: app.globalData.userInfo ? app.globalData.userInfo.name : '',
      faceData: app.globalData.userInfo ? app.globalData.userInfo.faceData : ''
    })
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2,
        list: translations.list
      });
    }
    if(wx.getStorageSync('userInfo').token) {
      this.getDeviceList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})